package com.example.prison.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.prison.config.Response;
import com.example.prison.entity.PersonnelManagement;
import com.example.prison.entity.UserInfo;
import com.example.prison.service.PersonnelManagementService;
import com.example.prison.service.SystemLogService;
import com.example.prison.service.UserInfoService;
import com.example.prison.util.TokenUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api("登录")
@RestController
@RequestMapping("/api")
public class LoginController {

    public static UserInfo loginPerson = new UserInfo();

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private PersonnelManagementService personnelService;

    @Autowired
    private SystemLogService systemLogService;

    @Autowired
    private HttpSession session;

    //登陆接口
    @ApiOperation("登录")
    @GetMapping("/login")
    public Map<String,Object> login(@Parameter(description = "用户名")@RequestParam(value = "userName") String userName,
                                    @RequestParam(value = "password") String password){
        log.info("登录");
        systemLogService.add("登录", userName);
        Map<String,Object> map = new HashMap<>();
        map.put("status","error");
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password) ){
            map.put("message","用户或者密码为空！");
            log.info("用户或者密码为空");
            return map;
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName)
                .eq("password",password);
        UserInfo userDb = userInfoService.getOne(queryWrapper);
        loginPerson = userDb;
        if(userDb != null){
            String token= TokenUtil.generateToken(userDb);
            map.put("status","success");
//            map.put("message","登录成功！");
            map.put("data",userDb);
            map.put("token",token);
            session.setAttribute("userName",userDb.getUserName());
        }else{
            map.put("message","用户名或密码错误！");
            log.info("用户名或密码错误");
        }
        return map;
    }

    public static String getOperator() {
        if(ObjectUtils.isEmpty(loginPerson.getUserName())) {
            return null;
        }
        return loginPerson.getUserName();
    }

//    //注册
//    @ApiOperation("注册")
//    @GetMapping("/register")
//    public Response<?>  register(@RequestParam(value = "userName") String userName,
//                                 @RequestParam(value = "password") String password,
//                                 @Parameter(description = "所属单位(1监狱2武警)") @RequestParam(value = "departmentId") Integer departmentId) {
//        log.info("注册");
//        if (StrUtil.isBlank(userName) || StrUtil.isBlank(password)) {
//            log.info("请输入用户名或密码");
//            return Response.error("请输入用户名或密码！");
//        } else {
//            UserInfo userDTO = new UserInfo();
//            userDTO.setUserName(userName);
//            userDTO.setPassword(password);
//            userDTO.setDepartmentId(departmentId);
//            int result = userInfoService.add(userDTO);
//            if (result == -1) {
//                log.info("用户已存在");
//                return Response.error("用户已存在！");
//            } else {
//                log.info("注册成功");
//                return Response.success("注册成功！");
//            }
//        }
//    }

    @ApiOperation("获取用户列表")
    @GetMapping("/getUser")
    public Response<PageInfo<UserInfo>> getUser(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取用户列表");
        PageInfo<UserInfo> pageInfo = userInfoService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public Response<?> addUser(@RequestBody UserInfo user) {
        log.info("新增用户");
        systemLogService.add("新增用户");
        int result = userInfoService.add(user);
        if (result == -1) {
            return Response.error("用户已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除用户")
    @PostMapping("/deleteUser")
    public Response<?> deleteUser(@Parameter(description = "用户ID") @RequestBody Integer[] ids) {
        log.info("删除用户");
        systemLogService.add("删除用户");
        userInfoService.delete(ids);
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑用户")
    @PostMapping("/updateUser")
    public Response<?> updateUser(@RequestBody UserInfo user) {
        log.info("编辑用户");
        systemLogService.add("编辑用户");
        Integer id = user.getUserId();
        int result = userInfoService.update(user, id);
        if (result == -1) {
            return Response.error("更新失败！用户已存在！");
        } else {
            return Response.success("更新成功！");
        }
    }

    @ApiOperation("依据id获取用户列表")
    @GetMapping("/getUserById")
    public Response<UserInfo> getUserById(@Parameter(description = "用户ID")@RequestParam(value = "userId") Integer userId) {
        log.info("依据id获取用户列表");
        systemLogService.add("依据id获取用户列表");
        UserInfo user = userInfoService.getById(userId);
        if(ObjectUtils.isEmpty(user)) {
            return Response.error("此用户id不存在！");
        }
        return Response.success("获取成功！", user);
    }

    @ApiOperation("依据姓名获取用户")
    @GetMapping("/getUserByName")
    public Response<PageInfo<UserInfo>> getUserByName(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @Parameter(description = "用户名")@RequestParam(value = "userName") String userName) {
        log.info("依据姓名获取用户");
        systemLogService.add("依据姓名获取用户");
        PageInfo<UserInfo> pageInfo = userInfoService.getByName(pageNumber, pageSize, userName);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此用户不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
